'''
Created on Aug 17, 2012

@author: petrina
'''
from link_indicator import Link_indicator
from indiutils import Indiutils
import logging


class Planning_time(Link_indicator):
    '''
    Calculates the planning time for every time interval (using aggregated fcd-data).
    
    The planning time is a kind of worst case travel time, e.g. which travel time does
    a commuter need to reach his/her destination in 95% of the cases. Therefore
    the planing time is equal to the 95th percentile of the travel times of a interval.
    The indicator calculates the planing time for each interval.

    unit: seconds (s)
    '''


    def __init__(self, link):
        '''        
        @param link: Link-object (containing valid records and timeseries) 
        '''
        
        Link_indicator.__init__(self, link) 
        self._name = 'Planning Time'
        self._mr_name =  'plan_time'
        self._unit = 's'
        #self.unit.__doc__ = 'seconds (s)'
        
        self._create_title(link.id, link.direction)
        
        logging.debug("Planning_time: created new Planning_time")
        

    #----------------------public methods of the class-------------------------

    def calculate(self):
        '''
        @return: planning time for every time interval as list of Result_values
        '''
        
        logging.info("Planning_time: start calculating indicator values")
        
        utils = Indiutils()
        self._values = utils.p95_tt(self._link.timeseries, self._link.length)
        
        logging.debug("Planning_time: finished calculating the planning time")
        return self._values
    
    
    def plot(self, plotobject):
        '''
        plots the planning time and the aggregated data 
        '''
        
        self._plot_data(plotobject, 'traveltime')